import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-18
 * Time: 14:34
 */
public class MyDoublyLinkedList implements IList{
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }

    }
    public ListNode head;
    public ListNode tail;
    @Override
    public void addFirst(int data) {
        ListNode Node = new ListNode(data);
        if(head == null){
            head = tail = Node;
            return;
        }
        Node.next = head;
        head.prev = Node;
        head = Node;
    }

    @Override
    public void addLast(int data) {
        ListNode Node = new ListNode(data);
        if(head == null){
            head = tail = Node;
            return;
        }
        tail.next = Node;
        Node.prev = tail;
        tail = Node;
    }
    private void CheckPosException(int pos){
        if(pos < 0 || pos > size()){
            throw new PosException("pos位置异常" + pos);
        }
    }

    @Override
    public void addIndex(int index, int data) {
        CheckPosException(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode Node = new ListNode(data);
        while(index != 0){
            cur = cur.next;
            index--;
        }
        Node.next = cur;
        Node.prev = cur.prev;
        cur.prev.next = Node;
        cur.prev = Node;
    }

    @Override
    public void remove(int key) {   //重点
       ListNode cur = head;
       if(cur == null){
           System.out.println("链表为空，不能删除");
       }
       while(cur != null){
           if(cur.val == key){
               if(cur == head){ //如果删除的是头结点
                   head = head.next;
                   if(head != null){    //防止只有一个节点
                       head.prev = null;
                   }
               }else{   //到这里说明不是头节点, 并且不只一个节点
                   cur.prev.next = cur.next;
                   if(cur.next == null) {
                       tail = tail.prev;    //删除尾巴节点
                   }else{
                       cur.next.prev = cur.prev; //删除中间节点
                   }
               }
               return;  //删除成功，直接结束循环
           }
           cur = cur.next;
       }
        System.out.println("没有找到你要删除的节点");
    }

    @Override
    public void removeAllKey(int key) {
        boolean flag = true;    //假设没找到删除元素
        ListNode cur = head;
        if(cur == null){
            System.out.println("链表为空，不能删除");
        }
        while(cur != null){
            if(cur.val == key){
                flag = false; //已经找到删除元素
                if(cur == head){ //如果删除的是头结点
                    head = head.next;
                    if(head != null){    //防止只有一个节点
                        head.prev = null;
                    }
                }else{   //到这里说明不是头节点, 并且不只一个节点
                    cur.prev.next = cur.next;
                    if(cur.next == null) {
                        tail = tail.prev;    //删除尾巴节点
                    }else{
                        cur.next.prev = cur.prev; //删除中间节点
                    }
                }
                //return;  //删除成功一个key，这里不直接结束，继续找到所有的删除完
            }
            cur = cur.next;
        }
        if(flag){
            System.out.println("没有找到你要删除的节点");
        }

    }

    @Override
    public int size() {
        ListNode cur = head;
        int cnt = 0;
        while(cur != null){
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    @Override
    public void clear() {
        if(head == null){
            System.out.println("链表已经为空");
            return;
        }
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
